package controleur;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.Adresse;
import model.Etudiant;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import businessObjects.AdresseBo;
import businessObjects.EtudiantBo;
import businessObjects.HistoriqueBo;
import businessObjects.SessionCouranteBo;

public final class ProfilAction extends Action {

	Etudiant unEtudiant = new Etudiant();
	Adresse uneAdresse = new Adresse();
	EtudiantBo unEtudiantBo = new EtudiantBo();
	AdresseBo uneAdresseBo = new AdresseBo();
	SessionCouranteBo uneSessionBo = new SessionCouranteBo();
	HistoriqueBo hisBo= new HistoriqueBo();
	
	public ActionForward execute(ActionMapping mapping, 
			ActionForm form, 
			HttpServletRequest req, 
			HttpServletResponse res) throws Exception {
		
		String page = req.getParameter("page");
		
		HttpSession session = req.getSession(true);
		
		int idEtudiant = (Integer)session.getAttribute("id");
		
		if(page.equals("infosPerso"))
		{
			System.out.print("Je suis dans perso "+idEtudiant);
			req.setAttribute("id",idEtudiant);
			
			req.setAttribute("etudiant",unEtudiantBo.getProfile(idEtudiant));
		
			req.setAttribute("adresse",uneAdresseBo.getAdresseByEtudiantId(idEtudiant));
			
			page = "infosPerso";
		}
		else if (page.equals("chronologieCours")){
			
			System.out.print("Id de etudiant "+idEtudiant);
			req.setAttribute("etudiant",unEtudiantBo.getProfile(idEtudiant));
			req.setAttribute("creditsInscrits",uneSessionBo.getNbCreditInscrit(idEtudiant));
			req.setAttribute("moyenneCumulative", hisBo.getMoyenneCumulative(idEtudiant));
			req.setAttribute("sessions", hisBo.getResultatByIdEtudiant(idEtudiant));
			
			page = "chronologieCours";
		}
		      
			return mapping.findForward(page);
	}
}